﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TZReport_Util.Util
{
    public sealed class DateUtils
    {
        public static long diffSeconds(DateTime DateTime1, DateTime DateTime2)
        {
            long seconds = (long)(DateTime1 - DateTime2).TotalSeconds;
            if (seconds < 0)
                seconds = Math.Abs(seconds);

            return seconds;
        }

        public static int diffMonth(DateTime dateTime1, DateTime dateTime2)
        {
            int diff = (dateTime1.Year - dateTime2.Year) * 12 + (dateTime1.Month - dateTime2.Month);
            if (diff < 0)
                diff = Math.Abs(diff);

            return diff;
        }

        public static long GetTimeStamp()
        {
            DateTime baseTime = Convert.ToDateTime("1970-01-01 08:00:00");//基准时间
            long newTime = (long)(DateTime.Now - baseTime).TotalSeconds;
            return newTime;
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStampStr()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds).ToString();
        }

        public static string GetTimeStampSeconds()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds / 1000).ToString();
        }


        public static bool ConvertDateTime(string date, out DateTime result, string format = "yyyy-MM-dd")
        {
            bool flag = DateTime.TryParseExact(date, format, null, DateTimeStyles.None, out result);
            return flag;
        }


        public static string GetWeek(string date)
        {
            DateTime dt = default(DateTime);
            DateTime.TryParse(date, out dt);
            return ConvertWeek(dt);
        }

        public static string GetWeek(DateTime dt)
        {
            return ConvertWeek(dt);
        }

        private static string ConvertWeek(DateTime dt)
        {
            string result = string.Empty;
            string str = string.Empty;
            if (dt == default(DateTime))
                str = string.Empty;
            else
                str = dt.DayOfWeek.ToString();
            switch (str)
            {
                case "Monday":
                    result = "星期一";
                    break;
                case "Tuesday":
                    result = "星期二";
                    break;
                case "Wednesday":
                    result = "星期三";
                    break;
                case "Thursday":
                    result = "星期四";
                    break;
                case "Friday":
                    result = "星期五";
                    break;
                case "Saturday":
                    result = "星期六";
                    break;
                case "Sunday":
                    result = "星期日";
                    break;
                default:
                    result = "空";
                    break;
            }

            return result;
        }

        public static List<string> GetAllMonthDate(int year, int month)
        {
            List<string> listDate = new List<string>();
            string startStr = year.ToString() + "-" + (month < 10 ? "0" + month.ToString() : month.ToString()) + "-01";
            DateTime start = DateTime.Parse(startStr);
            DateTime end = start.AddMonths(1);
            while (start < end)
            {
                string date = start.ToString("yyyy-MM-dd");
                listDate.Add(date);
                start = start.AddDays(1);
            }

            return listDate;
        }

        public static List<string> GetAllMonthDate(int year, int month, out DateTime startDate, out DateTime endDate)
        {
            List<string> listDate = new List<string>();
            string startStr = year.ToString() + "-" + (month < 10 ? "0" + month.ToString() : month.ToString()) + "-01";
            DateTime start = DateTime.Parse(startStr);
            startDate = start;
            DateTime end = start.AddMonths(1);
            endDate = end.AddDays(-1); // 下一个月的前一天
            while (start < end)
            {
                string date = start.ToString("yyyy-MM-dd");
                listDate.Add(date);
                start = start.AddDays(1);
            }

            return listDate;
        }

        public static List<string> GetAllYearDate(int year)
        {
            List<string> listDate = new List<string>();
            string startStr = year.ToString() + "-01-01";
            DateTime start = DateTime.Parse(startStr);
            DateTime end = start.AddYears(1);

            while (start < end)
            {
                string date = start.ToString("yyyy-MM-dd");
                listDate.Add(date);
                start = start.AddDays(1);
            }

            return listDate;
        }
    }
}
